<?php
/*
 * EmailComponent for CakePHP
 *
 * @author      gwoo <gwoo@rd11.com>
 * @version     0.10.8.2176
 * @license		OPPL
 *
 */
class ValidComponent extends Object
{
	function mbmaxlength($val, $arg1, $arg2 = null) {
		if (!empty ($val)) {
			if (empty ($arg2)) {
				if (mb_strlen($val, 'UTF-8') > $arg1) {
					return false;
				}
			} else {
				if (mb_strlen($val, 'UTF-8') > $arg1 * $arg2) {
					return false;
				}
			}
		}
		return true;
	}
	
	function regex($val,$arg1,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^".$arg1."+$/",$val)){
    			return false;
    		}
    	}
    	return true;
	}
	
    function maxlength($val,$arg1,$arg2=null)
    {
    	if(!empty($val)){
	    	if(strlen($val)>$arg1){
	    		return false;
	    	}
    	}
        return true; 
    }

    function minlength($val,$arg1,$arg2=null)
    {
    	if(!empty($val)){
	    	if(strlen($val)<$arg1){
	    		return false;
	    	}
    	}
        return true; 
    }

    function minmaxlength($val,$arg1,$arg2=null)
    {
    	$range = split('-',$arg1);
    	if(!empty($val)){
	    	if($range[0]>strlen($val) || strlen($val)>$range[1]){
	    		return false;
	    	}
    	}
        return true; 
    }
      
    function mbminmaxlength($val,$arg1,$arg2=null)
    {
    	$range = split('-',$arg1);
    	if(!empty($val)){
	    	if($range[0]>mb_strlen($val, 'UTF-8') || mb_strlen($val, 'UTF-8')>$range[1]){
	    		return false;
	    	}
    	}
        return true; 
    }
        
    function decimal($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^[0-9]+(.[0-9]{1,2})?$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }
    
    function number($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^\-?[0-9]+$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }

    function ansic($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^[!-■]+$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }
    
    function url($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^[A-Za-z0-9_-]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }
    
    function required($val,$arg1=null,$arg2=null){
    	
    	if(!is_array($val))
    		$val=trim($val);
    	if('0'!==$val && empty($val)){
    		return false;
    	}
    	return true;    	
    }


    function allRequired($val,$arg1=null,$arg2=null){
    	if(empty($val)){
    		return false;
    	}
    	return true;    	
    }
    
	function kana($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
			$chk = " アイウエオカキクケコサシスセソタチツテトナニヌネノ".
					"ハヒフヘホマミムメモヤユヨラリルレロワヲン".
					"ガギグゲゴザジズゼゾダヂヅデドバビブベボ".
					"パピプペポァィゥェォャュョーッ";
			if (!mbereg("^[".$chk."]+$", $val)) {
				return false;
			}
    	}
		return true;
	}
	
	function kana_roma($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
			$chk = " ァ-ンa-zA-Z";
			if (!mbereg("^[".$chk."]+$", $val)) {
				return false;
			}
    	}
		return true;
	}
    
    function date($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^([123456789][[:digit:]]{3})\-(0[1-9]|1[012])\-(0[1-9]|[12][[:digit:]]|3[01])$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }

    function time($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^(\d{2}):(\d{2})$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }

    function datetime($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^([123456789][[:digit:]]{3})-(0[1-9]|1[012])-(0[1-9]|[12][[:digit:]]|3[01])(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/",$val)){
    			return false;
    		}
    	}
    	return true;
    }

    function email($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/",$val)){
    			return false;
    		}
    	}
    	return true;    	
    }
    
    function must_one($val1=null,$val2=null){
    	$val1=trim($val1);
    	$val2=trim($val2);
    	if(empty($val1) && empty($val2)){
    		return false;
    	}
		return true;
    }

    function image_type($val,$arg1=null,$arg2=null)
    {
    	if(is_array($val)){
    		foreach($val as $type){
    			if(!empty($type)){
			    	if(stripos($type,'image')===false){
			    		return false;
			    	}
    			}
    		}
    	}
        return true; 
    }
    
    function upload_error($val,$arg1=null,$arg2=null){
    	if(is_array($val)){
    		foreach($val as $error){
    			if(!empty($error)){
			    	if($error!=4){
		    			return false;
		    		}
    			}
    		}
    	}
        return true; 
    }

    function file_size($val,$arg1,$arg2=null)
    {
    	if(is_array($val)){
    		foreach($val as $size){
    			if(!empty($size)){
			    	if($size>$arg1){
		    			return false;
		    		}
    			}
    		}
    	}
        return true; 
    }

    function file_size1($val,$arg1,$arg2=null)
    {
    	if(!empty($val)){
	    	if($val>$arg1){
    			return false;
		    }		    		
    	}
		if(empty($val)){
	    	return false;
	    }	
        return true; 
    }
    
    function file_type($val,$arg1,$arg2=null)
    {
    	if(!empty($val)){
	    	if(stripos($arg1,$val)===false){
	    		return false;
	    	}
    	}   	
		if(empty($val)){
			return false;
		}
		if(stripos($arg1,$val)===false){
		    return false;
		}
    	
        return true; 
    }
    
    function isChoose($data,$model,$field=null){
    	if(empty($field)){
	    	if(!isset($data[$model])){
	    		return false;
	    	}
    	}
    	else{
    		if(!isset($data[$model][$field])){
	    		return false;
	    	}
    	}
    	return true;
    }
    
    function hasOther($data1,$data2,$arg1,$arg2){
    	foreach($data1[$arg1] as $v){
    		if($v == 99 && !$this->required($data2[$arg2])){
    			return false;
    		}    			
    	}
    	return true;
    }

	function isString($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		if(!preg_match("/^[A-Za-z]+$/",$val)){
    			return false;
    		}
    		return true;
    	}
    	else{
    		return false;
    	}
    	
    }
	
	function password($data){
		if($this->number($data)||$this->isString($data)){
			$len=strlen($data);
			if($len < 8 || $len > 16){
				return false;
			}
			return true;
		}
		else{
			$len=strlen($data);
			if($len < 6 || $len > 16){
				return false;
			}
			return true;
		}
	}
	
	function compareDate($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		$pre_month = $this->pre_months($val);

    		$now_date = date("Y-m-d"); 
    		if( $val < $now_date || $pre_month < 0 ){ 
    			return false;
    		}
    	}
    	
    	return true; 		
	}
	
	function compareDate_2($val,$arg1=null,$arg2=null){
    	if(!empty($val)){
    		$now_date = date("Y-m-d");    		
    		if($val > $now_date){
    			return false;
    		}
    	}
    	
    	return true; 		
	}	

	var $config = null;

	var $filename = 'message.properties';
	
	var $columnList = array();

	function __construct(){
		$this->config = $this->readConfigFile(CONFIGS . $this->filename);
	}


	function ErrorMsgAllRequired($data, $column, $valid, $arg1=null,$arg2=null){
		if(isset($this->columnList[$column])) return;
		$message = '';		
		$val = isset($data[$column])?$data[$column]:'';
		if(!$this->$valid($val,$arg1,$arg2)) {
			$message .= $this->tagErrorMsg($valid,$column,$arg1,$arg2);
			$this->columnList[$column]=$valid;
		}
		return $message;
	}
		
	function ErrorMsg($data, $column, $valid, $arg1=null,$arg2=null){
		if(isset($this->columnList[$column])) return;

		$message = '';

		list($model, $field) = explode("/", $column);
		

		
		if($valid=='hasOther'){
			if(!$this->$valid($data[$model],$data[$field],$arg1,$arg2)){
				$message .= $this->tagErrorMsg($valid,$field.'/'.$arg2);
				$this->columnList[$column]=$valid;
			}
			return $message;
		}
		
		if($valid=='must_one'){
			if(!$this->$valid($data[$model][$field],$data[$arg1][$arg2])){
				$message .= $this->tagErrorMsg($valid,$column,$arg1.'/'.$arg2);
				$this->columnList[$column]=$valid;
			}
		}

		if($valid=='isChoose'){
			if(empty($arg1)){
				if(!$this->$valid($data,$model)){
					$message .= $this->tagErrorMsg($valid,$column);
					$this->columnList[$column]=$valid;
				}
			}
			else{
				if(!$this->$valid($data,$model,$field)){
					$message .= $this->tagErrorMsg($valid,$column);
					$this->columnList[$column]=$valid;
				}
			}
			
			return $message;
		}
		
		if($arg1=='partten'){
			$val = '';
			$val = $this->getPartten($data,$column);
			if(!$this->$valid($val,$arg1,$arg2)){
				$message .= $this->tagErrorMsg($valid,$column);
				$this->columnList[$column]=$valid;
			}
			return $message;
		}

		if($arg1=='parttendate'){
			$val = '';
			$val = $this->getParttenDate($data,$column);
			if(!$this->$valid($val,$arg1,$arg2)){
				$message .= $this->tagErrorMsg($valid,$column);
				$this->columnList[$column]=$valid;
			}
			return $message;
		}

		if($arg1=='parttentime'){
			$val = '';
			$val = $this->getParttenTime($data,$column);
			if(!$this->$valid($val,$arg1,$arg2)){
				$message .= $this->tagErrorMsg($valid,$column);
				$this->columnList[$column]=$valid;
			}
			return $message;
		}

		if($valid=='confirm'){
			if($data[$model][$field.'_confirm'] != $data[$model][$field]) {
				$message .= $this->tagErrorMsg($valid,$column);
				$this->columnList[$column]=$valid;
			}
			return $message;
		}

		if($valid=='less'){
			list($model2, $field2) = explode("/", $arg1);
			if($data[$model][$field] > $data[$model2][$field2]) {
				$message .= $this->tagErrorMsg($valid,$column,$arg1);
				$this->columnList[$column]=$valid;
			}
			return $message;
		}
	
		$val = isset($data[$model][$field])?$data[$model][$field]:'';
		if(!$this->$valid($val,$arg1,$arg2)) {
			$message .= $this->tagErrorMsg($valid,$column,$arg1,$arg2);
			$this->columnList[$column]=$valid;
		}
		return $message;

		
		
	}

	function tagErrorMsg($error,$arg0=null,$arg1=null,$arg2=null)
	{
		if(isset($error))
		{
			if(isset($arg0))
			$arg0 = isset($this->config[low("label.$arg0")])?$this->config[low("label.$arg0")]:$arg0;
			if(isset($arg1))
			$arg1 = isset($this->config[low("label.$arg1")])?$this->config[low("label.$arg1")]:$arg1;
			if(isset($arg2))
			$arg2 = isset($this->config[low("label.$arg2")])?$this->config[low("label.$arg2")]:$arg2;

			$message = '';
			$message = isset($this->config[low("error.$error")])?$this->config[low("error.$error")]:$error;
			$message = r('{0}',$arg0,$message);
			$message = r('{1}',$arg1,$message);
			$message = r('{2}',$arg2,$message);
			$message = " " . $message . "<br/>";

			return $message;
		}
		return false;
	}
	
	function getParttenDate($data,$column){
		list($model, $field) = explode("/", $column);
		if(!isset($data[$model][$field.'_year'])) return '';
		if(!isset($data[$model][$field.'_month'])) return '';
		if(!isset($data[$model][$field.'_day'])) return '';
		if(empty($data[$model][$field.'_year']) 
		  && empty($data[$model][$field.'_month'])
		  && empty($data[$model][$field.'_day'])){
		  	return '';
		  }
		$zero_month = strlen($data[$model][$field.'_month'])==1?'0':'';
		$zero_day = strlen($data[$model][$field.'_day'])==1?'0':'';
		return $val = 	$data[$model][$field.'_year']
						.'/'.$zero_month.$data[$model][$field.'_month']
						.'/'.$zero_day.$data[$model][$field.'_day'];
	}
	
	function getParttenTime($data,$column){
		list($model, $field) = explode("/", $column);
		if(!isset($data[$model][$field.'_hour'])) return '';
		if(!isset($data[$model][$field.'_min'])) return '';
		return $val = 	$data[$model][$field.'_hour']
						.':'.$data[$model][$field.'_min'];
	}
	
	function getPartten($data,$column){
		list($model, $field) = explode("/", $column);
		$val = '';
		if(isset($data[$model][$field.'1'])) {
			$val .= $data[$model][$field.'1'];
		}
		if(isset($data[$model][$field.'2'])) {
			$val .= $data[$model][$field.'2'];
		}
		if(isset($data[$model][$field.'3'])) {
			$val .= $data[$model][$field.'3'];
		}
		if(isset($data[$model][$field.'4'])) {
			$val .= $data[$model][$field.'4'];
		}
		if(isset($data[$model][$field.'5'])) {
			$val .= $data[$model][$field.'5'];
		}
		return $val;
	}

	function extractParttenDate($data,$column){
		list($model, $field) = explode("/", $column);
		if(!isset($data[$model][$field])) return '';
		list($date,$time) = explode(" ",$data[$model][$field]);
		$data[$model][$field] = $date;
		list($year, $month, $day) = explode("-",$date);
		$data[$model][$field.'_year'] = $year;
		$data[$model][$field.'_month'] = $month;
		$data[$model][$field.'_day'] = $day;
		return $data;
	}

	function extractParttenTime($data,$column){
		list($model, $field) = explode("/", $column);
		if(!isset($data[$model][$field])) return '';
		list($hour, $min) = explode(":",$data[$model][$field]);
		$data[$model][$field.'_hour'] = $hour;
		$data[$model][$field.'_min'] = $min;
		return $data;
	}

	function extractPartten($data,$column,$partten){
		list($model, $field) = explode("/", $column);
		if(!isset($data[$model][$field])) return '';
		$vals = explode($partten,$data[$model][$field]);
		foreach ($vals as $key => $val){
			$data[$field.$key] = $val;
		}
		return $data;
	}

	function getOptionValue($data,$column,$list){
		list($model, $field) = explode("/", $column);
		if(!isset($data[$model][$field])) return '';
		$val = $data[$model][$field];
		return isset($list[$val])?$list[$val]:'';
	}

	function convertAlphaNumber($string){
		$string = str_replace(array('０','１','２','３','４','５','６','７','８','９','Ａ','Ｂ','Ｃ','Ｄ','Ｅ','Ｆ','Ｇ','Ｈ','Ｉ','Ｊ','Ｋ','Ｌ','Ｍ','Ｎ','Ｏ','Ｐ','Ｑ','Ｒ','Ｓ','Ｔ','Ｕ','Ｖ','Ｗ','Ｘ','Ｙ','Ｚ','ａ','ｂ','ｃ','ｄ','ｅ','ｆ','ｇ','ｈ','ｉ','ｊ','ｋ','ｌ','ｍ','ｎ','ｏ','ｐ','ｑ','ｒ','ｓ','ｔ','ｕ','ｖ','ｗ','ｘ','ｙ','ｚ','＠','．','：','／','＿','？','＆','ー','－','―','％','＋'),
							array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','@','.',':','/','_','?','&','-','-','-','%','+'),
							$string);
	    
	    return $string;
	}
	
	function getCheckBoxesValue($data,$column,$checkbox){

		$checkboxName = array();
		list($model, $field) = explode("/", $column);

		if(isset($data[$model][$field])){
		foreach($data[$model][$field] as $key => $checkValue){
			$checkboxKeys[] = $checkValue;
			$checkboxValues[] = $checkbox[$checkValue];
		}
		
		$checkboxName = array_combine($checkboxKeys,$checkboxValues);
		}
		return $checkboxName;
	}
	
	function str_number($val, $arg1 = null, $arg2 = null) {
		if (!empty ($val)) {
			if (!preg_match("/^[A-Za-z0-9_-]+$/", $val)) {
				return false;
			}
		}
		return true;
	}
	
	function tel_number($val, $arg1 = null, $arg2 = null) {
		if (!empty ($val)) {
			if (!preg_match("/^[A-Za-z0-9_-+#/(/)]+$/", $val)) {
				return false;
			}
		}
		return true;
	}
	
	function label($key)
	{
		return $this->getValue('label.'.$key);
	}

	function error($key)
	{
		return $this->getValue('error.'.$key);
	}

	function getValue($key)
	{
		if(!empty($key))
		{
			return ($this->config[low($key)]);
		}
		return false;
	}

	function readConfigFile($fileName) {
		$fileLineArray = file($fileName);

		foreach($fileLineArray as $fileLine) {
			$dataLine = trim($fileLine);
			$firstChar = substr($dataLine, 0, 1);

			if ($firstChar != '#' && $dataLine != '') {
				if ($firstChar == '[' && substr($dataLine, -1, 1) == ']') {
					// [section block] we might use this later do not know for sure
					// this could be used to add a key with the section block name
					// but it adds another array level
				} else {
					$delimiter = strpos($dataLine, '=');

					if ($delimiter > 0) {
						$key = strtolower(trim(substr($dataLine, 0, $delimiter)));
						$value = trim(substr($dataLine, $delimiter + 1));

						if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
							$value = substr($value, 1, -1);
						}

						$iniSetting[$key] = stripcslashes($value);
					} else {
						$iniSetting[strtolower(trim($dataLine))] = '';
					}
				}
			} else {
			}
		}

		return $iniSetting;
	}
	
	
}

?>